package com.etc.service.impl;

import com.etc.dao.CityDao;
import com.etc.entity.City;
import com.etc.redis.ProvCityAreaKey;
import com.etc.redis.RedisService;
import com.etc.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文件名:com.etc.service.impl.CityServiceImpl
 * 描述：城市业务实现
 * 作者:吴仕泉
 * 时间:2019/5/26 15:54
 */
@Service
public class CityServiceImpl implements CityService {

    @Autowired
    RedisService redisService;

    @Autowired
    CityDao cityDao;

    //根据城市编号，查询该市的所有地区信息
    @Override
    public List<City> findCitysByProvId(Integer proNum) {
        //从Redis缓存中读取所需信息
        List<City> citys = redisService.getList(ProvCityAreaKey.allCityKey,String.valueOf(proNum));
        //缓存中不存在该信息
        if(citys == null){
            //从数据库中读取信息
            citys = cityDao.listSelectCityByProvinceNum(proNum);
            if (citys == null){
                return null;
            }
            redisService.setList(ProvCityAreaKey.allCityKey,String.valueOf(proNum),citys);
        }
        return  citys;
    }

    //根据城市编号查询城市的信息
    @Override
    public City findCityByCityNum(Integer cityNum) {
        City city = redisService.get(ProvCityAreaKey.oneCityKey,String.valueOf(cityNum),City.class);
        if(city == null){
           city = cityDao.getCityMessageByCityNum(cityNum.longValue());
           if(city != null){
               redisService.set(ProvCityAreaKey.oneCityKey,String.valueOf(cityNum),city);
           }
        }
        return city;
    }
}
